**
** processed on server
** 2021-02-25
** T:\bb_lake_chad_rem\prep_ntl_viirs_server_ts.r
**

clear

// Get the path of the currently running do-file
local do_file_path `c(pwd)'

// Change the working directory to the location of the do-file
cd "`do_file_path'"

** lit mask
cd "proc_data\ntl_viirs"

use "ntl_viirs_lit_masked20210301.dta"
rename *, lower

keep objectid gid_0 ntlv2012-ntlv2020

** panel
reshape long ntlv, i(objectid gid_0) j(year) 
rename ntlv ntl2ltmk

label variable objectid "Seq id per grid or split grid at country border"
label variable gid_0 "Country code from admin shp : GADM v3.6"
label variable ntl2ltmk "Mean of grid of annual lit masked NTL (VIIRS v2) [0,1] : EOG"
label variable year "year of NTL v2 composite, starting 2012-04... : EOG"
save "NTL_VIIRS_litmask_ts1220.dta", replace

**
** Average masked
**

clear
use "ntl_viirs_average_masked20210301.dta"
rename *, lower

keep objectid gid_0 ntlv2012-ntlv2020

** panel
reshape long ntlv, i(objectid gid_0) j(year) 
rename ntlv ntl2mnmk

label variable objectid "Seq id per grid or split grid at country border"
label variable gid_0 "Country code from admin shp : GADM v3.6"
label variable ntl2mnmk "Mean of grid of annual masked NTL (VIIRS v2) (radiance + or - : nanoWatts/cm2/sr) : EOG"
label variable year "year of NTL v2 composite, starting 2012-04... : EOG"
save "NTL_VIIRS_meanmask_ts1220.dta", replace

**
** average
**

clear

use "ntl_viirs_average20210301.dta"
rename *, lower

keep objectid gid_0 ntlv2012-ntlv2020

** panel
reshape long ntlv, i(objectid gid_0) j(year) 
rename ntlv ntl2mn

label variable objectid "Seq id per grid or split grid at country border"
label variable gid_0 "Country code from admin shp : GADM v3.6"
label variable ntl2mn "Mean of grid of mean annual NTL (VIIRS v2) (radiance + or - : nanoWatts/cm2/sr) : EOG"
save "NTL_VIIRS_meanonly_ts1220.dta", replace


**
** Median masked
**
clear

use "ntl_viirs_median_masked20210301.dta"
rename *, lower

keep objectid gid_0 ntlv2012-ntlv2020

** panel
reshape long ntlv, i(objectid gid_0) j(year) 
rename ntlv ntl2mdmk

label variable objectid "Seq id per grid or split grid at country border"
label variable gid_0 "Country code from admin shp : GADM v3.6"
label variable ntl2mdmk "Mean of grid of median annual masked NTL (VIIRS v2) (radiance + or - : nanoWatts/cm2/sr) : EOG"
label variable year "year of NTL v2 composite, starting 2012-04... : EOG"
save "NTL_VIIRS_medianmask_ts1220.dta", replace


**
** Median
**
clear
use "ntl_viirs_median20210301.dta"
rename *, lower

keep objectid gid_0 ntlv2012-ntlv2020

** panel
reshape long ntlv, i(objectid gid_0) j(year) 
rename ntlv ntl2md

label variable objectid "Seq id per grid or split grid at country border"
label variable gid_0 "Country code from admin shp : GADM v3.6"

label variable ntl2md "Mean of grid of annual median NTL (VIIRS v2) (radiance + or - : nanoWatts/cm2/sr) : EOG"

label variable year "year of NTL v2 composite, starting 2012-04... : EOG"
save "NTL_VIIRS_median_ts1220.dta", replace


**
** merge 
**

merge 1:1 objectid year using "NTL_VIIRS_medianmask_ts1220.dta"
drop _merge

merge 1:1 objectid year using "NTL_VIIRS_meanmask_ts1220.dta"
drop _merge

merge 1:1 objectid year using "NTL_VIIRS_meanonly_ts1220.dta"
drop _merge

merge 1:1 objectid year using "NTL_VIIRS_litmask_ts1220.dta"
drop _merge


label variable year "year of NTL v2 composite, starting 2012-04... : EOG"

save "..\NTL_VIIRS_vars_1220.dta", replace
